package com.fy.fyai.common.utils;

/**
 * AI聊天提示优化工具
 */
public class AIChatOptimize {

    // 仅用于优化提问：去除<think>、换行、多余空格，保证向量检索文本干净
    public static String cleanForEmbedding(String text, String question) {
        if (text != null && !text.trim().isEmpty()) {
            // 移除<think>标签及其内容（支持多行）
            text = text.replaceAll("(?s)<think>.*?</think>", "").trim();
            // 移除可能的思考前缀
            text = text.replaceAll("^(思考|分析|优化后|回答|问题|提问)[：:]*", "").trim();
            // 清理文本：移除换行符、多余空格
            text = text.replaceAll("\\s+", " ").trim();
            if (text.isEmpty()) {
                text = question;
            }
        } else {
            text = question;
        }
        return text;
    }

    // 仅用于最终回答：只做敏感词过滤，保留换行和markdown格式
    public static String filterSensitiveWords(String text) {
        if (text == null) return null;
        String[] sensitivePatterns = {
                "(?i)知识库[\\w\\s]*", "(?i)提示词[\\w\\s]*", "(?i)markdown[\\w\\s]*",
                "(?i)系统实现[\\w\\s]*", "(?i)开发者[\\w\\s]*", "(?i)prompt[\\w\\s]*", "(?i)数据来源[\\w\\s]*",
                "(?i)knowledge base[\\w\\s]*", "(?i)prompt[\\w\\s]*", "(?i)markdown[\\w\\s]*", "(?i)developer[\\w\\s]*", "(?i)system implementation[\\w\\s]*", "(?i)data source[\\w\\s]*"
        };
        for (String pattern : sensitivePatterns) {
            text = text.replaceAll(pattern, "资料");
        }
        String[] selfExpose = {"作为AI助手", "根据我的知识", "我是AI模型", "作为人工智能", "根据我的训练数据", "As an AI assistant", "As an AI model", "According to my knowledge", "Based on my training data"};
        for (String word : selfExpose) {
            text = text.replaceAll(word, "");
        }
        return text;
    }
}
